package com.bjsdzk.collector.utils;

public class StrUtils {

    public static String getSmall(String data) {
        int length = data.length();
        String result="";
        for (int i = length; i >0 ; i-=2) {
            String substring = data.substring(i - 2, i);
            result=result.concat(substring);
        }
        return result;
    }

    public static String decode(int[] ints, String splicData) {
        byte[] dataBytes = getByteArray(splicData);
        String data = getDecodeStr(ints, dataBytes);
        return data;
    }

    private static String getDecodeStr(int[] key, byte[] data) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < data.length; i++) {
            int res = data[i] & 0x00ff ^ key[i % 5];
            sb.append(String.format("%02x", res));
        }
        return sb.toString();
    }

    public static byte[] getByteArray(String data) {
        byte[] bytes = data.getBytes();
        int l = bytes.length;
        byte[] result = new byte[l / 2];
        for (int i = 0, j = 0; i < l; i += 2, j++) {
            byte a = Byte.decode("0x" + new String(new byte[]{bytes[i]})).byteValue();
            byte b = Byte.decode("0x" + new String(new byte[]{bytes[i + 1]})).byteValue();
            result[j] = (byte) (((a << 4) & 0xf0) | (b & 0x0f));
        }
        return result;
    }
}
